// pages/school/rank/index.js
const selectorRouteDatas = [{
    id: 0,
    title: "校友会",
    type: "radiobox",
    key: "rankType",
    items: []
  },
  {
    id: 1,
    title: "综合排序",
    type: "radiobox",
    key: "rankClassification",
    items: []
  },

]
const {
  schoolRankList,
  schoolRankType,
  schoolRank,
} = require('../../../api/api')
const {
  decodeUserInfo
} = require('../../../api/login')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total:0,
    selectorDataList: selectorRouteDatas,
    filterRouteKey: 'schoolRankFilters',
    selectedData: {},
    pageNo:1,
    pageSize:20,
    tableData: [],
    list:[],
    schoolName:null,
    columns: [{
        label: '排 名',
        prop: 'rank',
        width: '120',
        align: 'center',
      },
      {
        label: '院 校',
        prop: 'schoolName',
        align: 'center',
        width: '230',
        isFilter:true
      },
      {
        label: '类 型',
        prop: 'rankClassification',
        align: 'center',
        width: 160,
        padding: '20rpx',
      },
      {
        label: '地 区',
        prop: 'provinceName',
        width: 140,
        align: 'center'
      },
    ],
    setting: {
      theadHeight: '80rpx',
    },
    bottomLift: getApp().globalData.bottomLift,
    isVip:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getSchoolRank()
  },
  inputChange(e){
    this.setData({
      schoolName:e.detail.value
    })
  },
  search(){
    this.onLoadFunc()
    // const {schoolName,list}=this.data
    // let newList = new Array()
    // if(schoolName){
    //   newList=list.filter(item=>{
    //     return item.schoolName.indexOf(schoolName)>-1
    //   })
    // }else{
    //   newList=list
    // }
    // this.setData({
    //   tableData:newList
    // })
  },
  async getSchoolRank() {
    const {
      result
    } = await schoolRank()
    const {
      selectorDataList,
      selectedData
    } = this.data
    const list = result.map((item) => {
      return {
        id: item,
        title: item
      }
    })
    selectorDataList[0].items = list
    selectedData['rankType'] = result[0]
    this.setData({
      selectorDataList,
      selectedData
    })
    this.getSchoolCategory(result[0])
  },
  async getSchoolCategory(rankType) {
    const {
      result
    } = await schoolRankType(rankType)
    const {
      selectorDataList,
      filterRouteKey,
      selectedData
    } = this.data
    if(result){
      const list = result.map((item, index) => {
        return {
          id: item,
          title: item,
        }
      })
      selectorDataList[1].items = list
      selectedData['rankClassification'] = result[0]
    }else{
      selectorDataList[1].items = []
      selectedData['rankClassification'] = ''
    }
    this.setData({
      selectorDataList,
      selectedData
    })
    wx.setStorageSync(filterRouteKey, selectedData)
    this.filterKeyFunc()
  },
  filterKeyFunc() {
    const {
      filterRouteKey
    } = this.data
    const selectedData = wx.getStorageSync(filterRouteKey)
    const {
      selectorDataList
    } = this.data
    selectorDataList.map((item) => {
      const c = item.items.filter((child) => {
        return child.id == selectedData[item.key]
      })
      item.title = c.length > 0 ? c[0].title : ''
    })
    this.setData({
      selectorDataList,
      selectedData
    })
    this.onLoadFunc()

  },
  onLoadFunc() {
    this.setData({
      pageNo: 1,
      tableData: []
    })
    this.getSchoolRankList()
  },
  async onSelectChangedRoute(e) {
    const {
      filterRouteKey
    } = this.data
    const {
      selectedData,selectorDataList
    } = this.data
    wx.setStorageSync(filterRouteKey, {
      ...e.detail
    });
    if (selectedData.rankType !== e.detail.rankType) {
      this.setData({
        selectedData: e.detail
      })
      selectorDataList[0].title=e.detail.rankType
      this.setData({
        selectorDataList
      })
      this.getSchoolCategory(e.detail.rankType)
      return
    }
    this.filterKeyFunc()
  },

  async getSchoolRankList() {
    const {
      selectedData,
      pageNo,
      pageSize,
      schoolName,
      tableData
    } = this.data
    selectedData.pageNo=pageNo
    selectedData.pageSize=pageSize
    selectedData.schoolName=schoolName?schoolName:''
    // console.log(selectedData)
    const {
      result
    } = await schoolRankList(selectedData)
    this.setData({
      tableData:tableData.concat(result.records),
      pageNo:pageNo+1,
      total:result.total
    })
  },
  
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const {total,tableData}=this.data
    if(total == tableData.length)return
    this.getSchoolRankList()
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

   /**
   * 生命周期函数--监听页面显示
   */
  onShow(){
    this.fetchUserInfo()
  },
  async fetchUserInfo() {
    const result= await getApp().fetchUserInfo()
    this.setData({
       vipType:result.vipType,
       isVip:result.vips.length>0?true:false,
   }) 
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '德道升学',
      imageUrl:'https://ehsimg.oss-cn-beijing.aliyuncs.com/images/static/users/shareImg.jpg'
    }
  }
})